/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.uds.android.fitdroid.demo.activity.spatial;

import org.uds.android.fitdroid.demo.apis.R;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

/**
 * Example of how to use an {@link com.google.android.maps.MapView}. <h3>
 * MapViewDemo</h3>
 * 
 * <p>
 * This demonstrates creating a Map based Activity.
 * </p>
 * 
 * <h4>Demo</h4> Views/MapView
 * 
 * <h4>Source files</h4>
 * <table class="LinkTable">
 * <tr>
 * <td >src/com.example.android.apis/view/MapViewDemo.java</td>
 * <td >The Alert Dialog Samples implementation</td>
 * </tr>
 * <tr>
 * <td >/res/layout/mapview.xml</td>
 * <td >Defines contents of the screen</td>
 * </tr>
 * </table>
 */
public class MapViewDemo extends MapActivity implements LocationListener {

	private MapView map = null;
	private MapController mapCon;

	// ???
	final private int menuMode = Menu.FIRST;
	final private int menuExit = Menu.FIRST + 1;
	private double latitude;
	private double longitude;
	private double altitude;
	private LocationManager locMgrGPS;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		map = (MapView) findViewById(R.id.map);

		map.setTraffic(true);
		map.setSatellite(false);
		map.setStreetView(true);

		map.setBuiltInZoomControls(true);

		GeoPoint geoSaar = new GeoPoint((int) (49.2495 * 1000000),
				(int) (1.999 * 1000000));
		mapCon = map.getController();
		
		locMgrGPS = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);
		locMgrGPS.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 1,
				this);

		Location location = locMgrGPS
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		GeoPoint geoCurrent = new GeoPoint((int) (location.getLatitude() * 1000000),
				(int) (location.getLongitude() * 1000000));
		mapCon.animateTo(geoCurrent);
		mapCon.setZoom(17);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// ????
		menu.add(0, menuMode, 0, "????");
		menu.add(0, menuExit, 1, "quit");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case menuExit:
			finish();
			break;

		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		this.latitude = location.getLatitude();
		this.longitude = location.getLongitude();
		this.altitude = location.getAltitude();
		mapCon.animateTo(new GeoPoint((int) (latitude * 1000000),
				(int) (longitude * 1000000)));
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}
}
